package com.wiloon.java8x.ssh;

import com.wiloon.java8x.PropertiesHelper;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.transport.verification.HostKeyVerifier;
import net.schmizz.sshj.xfer.FileSystemFile;

import java.io.IOException;
import java.security.PublicKey;


/**
 * This example demonstrates downloading of a file over SCP from the SSH server.
 */
public class SCPDownload {

    public static void main(String[] args)
            throws IOException {
        SCPDownload scpDownload = new SCPDownload();
        scpDownload.download();
    }

    public void download() {
        try {
            SSHClient ssh = new SSHClient();
            ssh.addHostKeyVerifier(new NullHostKeyVerifier());
            // ssh.useCompression(); // Can lead to significant speedup (needs JZlib in classpath)

            ssh.loadKnownHosts();

            ssh.connect(PropertiesHelper.getProperty("server.link.before9.ip"));
            try {
                // ssh.authPublickey(System.getProperty("root"));
                ssh.authPassword(PropertiesHelper.getProperty("server.link.before9.username"), PropertiesHelper.getProperty("server.link.before9.password").toCharArray());
                ssh.newSCPFileTransfer().download("/data/logs/xxx/debug.log", new FileSystemFile("d://debug.log"));
            } finally {
                ssh.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class NullHostKeyVerifier implements HostKeyVerifier {
        @Override
        public boolean verify(String arg0, int arg1, PublicKey arg2) {
            return true;
        }
    }
}